import Vue from 'vue'
// import store from '../store'
//1. 引入路由包
import Router from 'vue-router'
import VueRouter from 'vue-router';

import CategoryVue from "../views/CategoryPage.vue"
import ShoppingCart from "../views/ShoppingCart.vue"
import MyPage from "../views/MyPage.vue"
import GoodsDetail from "../views/GoodsDetail.vue"
import LoginPage from "../views/LoginPage.vue"
import RegisterPage from "../views/RegisterPage.vue"
import VerificationCode from '../views/VerificationCode.vue'
import RegpwdPage from '../views/RegpwdPage.vue'
import SetPage from '../views/SetPage.vue'
import CategoryResult from '../views/CategoryResult.vue'
import OrderPage from '../views/OrderPage.vue'
import OrderAddress from '../views/OrderAddress.vue'
//2. 安装插件包到Vue上,
Vue.use(Router)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}


//5.导出路由实例，让它去控制vue根
let router = new Router({
    // mode:'hash'//哈希模式   location.href
    //mode: 'history',//历史记录   history.pushState
    routes: [
        {
            path: '/',
            redirect: 'home'
        },
        {
            path: '/home',
            component: () => import(/*webpackChunkName:'index'*/'@/views/HomePage')
        },
        {
            path: '/category',
            component: CategoryVue
        },
        {
            path: '/ShoppingCart',
            component: ShoppingCart,
            // 路由元信息
            meta: {
                requireAuth: true
            }
        },
        {
            path: '/MyPage',
            component: MyPage,
            // 路由独享守卫
            beforeEnter: (to, from, next) => {
                if (!sessionStorage.getItem('token')) {
                    next("/Login")
                }
                next()
            }
        },
        {
            name: 'GoodsDetail',
            path: '/GoodsDetail/:proid*',
            component: GoodsDetail
        },
        {
            path: '/Login',
            component: LoginPage
        },
        {
            path: '/register',
            component: RegisterPage
        },
        {
            path: '/register/sms',
            component: VerificationCode
        },
        {
            path: '/register/pwd',
            component: RegpwdPage
        },
        {
            path: '/set',
            component: SetPage
        },
        {
            path: '/serch',
            component: () => import(/*webpackChunkName:'search'*/'@/views/SearchPage')
        },
        {
            path: '/searchResult',
            component: () => import("@/views/SearchResult")
        },
        {
            path: '/categoryResult',
            component: CategoryResult
        },
        {
            path: '/order',
            component: OrderPage
        },
        {
            path: '/orderAddress',
            component: OrderAddress
        },

    ]
})
// 前置钩子
router.beforeEach((to, from, next) => {
    // 路由元信息
    if (to.meta.requireAuth) {
        if (!sessionStorage.getItem('token')) {
            next("/Login?path=" + to.path)
        }
    }
    next()
})
// 后置钩子 跳转之后 组件加载之前
// router.afterEach((to,from)=>{
// 全局后置守卫
// })
export default router